package com.hunliji.monitor.share.component.db.model;

import com.hunliji.common.exception.ErrorException;


import com.hunliji.monitor.share.utils.AesUtil;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * @author wuchen
 */
@Data
public class DatabaseConnectProperty {
    private final static Logger log = LoggerFactory.getLogger(DatabaseConnectProperty.class);

    /**
     * 连接驱动，默认mysql
     */
    private String dbDriver;

    /**
     * 数据库地址
     */
    private String host;

    /**
     * 数据库名称
     */
    private String name;

    /**
     * 账号
     */
    private String userName;

    /**
     * 密码
     */
    private String password;

    public static DatabaseConnectProperty build(String host, String tableName,String username, String secretKey,String password) {
        DatabaseConnectProperty property = new DatabaseConnectProperty();
        property.setHost(host);
        property.setName(tableName);
        property.setUserName(username);
        try {
            property.setPassword(AesUtil.decrypt(secretKey,password));
        } catch (Exception e) {
            log.error("occur an exception: ", e);
            throw new ErrorException(e.getMessage());
        }

        return property;
    }
}
